package com.nari.ncps.screen.controller.emergency;

import com.nari.ncps.screen.common.pojo.ApiResponse;
import com.nari.ncps.screen.entity.ConfigDatasourceInfo;
import com.nari.ncps.screen.entity.PadEnvInfo;
import com.nari.ncps.screen.service.ConfigDatasourceInfoService;
import com.nari.ncps.screen.service.PadEnvInfoService;
import com.nari.ncps.screen.utils.GlobalDataSourceState;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器，控制应急数据源
 * </p>
 *
 * @author dongxiaoxu
 * @since 2025-09-23
 */
@Tag(name = "开关")
@RestController
@RequestMapping("/emergency")
public class SwitchController {

    @Autowired
    private GlobalDataSourceState dataSourceState;


    @Autowired
    private ConfigDatasourceInfoService configDatasourceInfoService;

    @GetMapping("/switch")
//    public ApiResponse<Object> switchMode(@RequestParam boolean enable) {
    public ApiResponse<Object> switchMode( @RequestParam(value = "enable" ,required = false) Boolean enable) {

        //传false 关闭应急数据源 ，选择用默认数据源.
        //传true 开始应急数据源，选择用应急数据源.

        if(enable) {
            dataSourceState.setCurrentDataSource("primary");

        } else {
            dataSourceState.setCurrentDataSource("secondary");
        }

        return ApiResponse.succeed();
    }

    @GetMapping("/status")
    public  ApiResponse<ConfigDatasourceInfo> getStatus() {
        return ApiResponse.succeed(dataSourceState.getCurrentDataSource());
    }


    @Autowired
    private PadEnvInfoService padEnvInfoService;

    @Operation(summary = "查看当前使用ip和端口详情", description = "查看当前使用ip和端口详情")
    @GetMapping("/ipInfo")
    public ApiResponse<PadEnvInfo> ipInfo() {
        System.out.println("进入web-access层 ... ip状态信息");
        PadEnvInfo envInfo = padEnvInfoService.ipInfo();
        return ApiResponse.succeed(envInfo);
    }

    @Operation(summary = "设置ip和端口", description = "设置ip和端口")
    @GetMapping("/ipSet")
    public ApiResponse<PadEnvInfo> ipSet(
            @RequestParam(value = "ip" ,required = false) String ip,
            @RequestParam(value = "port" ,required = false) String port,
            @RequestParam(value = "desc" ,required = false) String desc) {
        System.out.println("进入web-access层 ... 设置ip信息");
        PadEnvInfo envInfo = padEnvInfoService.ipSet(ip,port,desc);
        return ApiResponse.succeed(envInfo);
    }




}
